package com.hkbigdata.springbootdi.mapper.impl;

import com.hkbigdata.springbootdi.mapper.ESDao;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;

/**
 * @author liuanbo
 * @creat 2024-12-13-10:38
 * @see 2194550857@qq.com
 */
/**
 * Repository类似mapper
 * Repository 使用与所有的数据库 无论是关系型数据库还是非关系型数据库
 * Mapper 适合操作JDBC中mybatis 的dao
 */
@Repository
public class ESDaoImpl implements ESDao {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public <T> SearchHits<T> queryFromEs(AbstractAggregationBuilder agg, Class<T> t) {
        //构建查询对象
        NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(agg).build();
        SearchHits<T> search = elasticsearchRestTemplate.search(query, t);
        return search;
    }
}
